package com.zx.mobile.controller;

import com.zx.exception.GlobalServiceException;
import com.zx.map.service.BaseAcctInfoService;
import com.zx.map.service.ConsumeLogService;
import com.zx.map.service.MerchantInfoService;
import com.zx.map.service.MessageSendLogService;
import com.zx.map.service.model.BaseAcctInfoModel;
import com.zx.map.service.model.ConsumeLogModel;
import com.zx.map.service.model.MerchantInfoModel;
import com.zx.mobile.MobileCommonHelper;
import com.zx.mobile.domain.pingan.*;
import com.zx.mobile.service.proxy.MessageSendProxy;
import com.zx.mobile.service.proxy.PASendVerifyCodeCommonProxy;
import com.zx.mobile.service.proxy.PAUserAcctChargeCommonProxy;
import com.zx.mobile.service.proxy.PAUserAcctQueryCommonProxy;
import com.zx.scp.service.model.MessageResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 对接平安 服务接口 Created by IntelliJ IDEA. User: Administrator Date: 15-7-5 Time:
 * 下午8:09 To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/mobile/PingAn")
public class PingAnGameController {
    /**
     * log
     */
    private static Logger log = Logger.getLogger(PingAnGameController.class);

    @Autowired
    private BaseAcctInfoService baseAcctInfoService;
    @Autowired
    private MerchantInfoService merchantInfoService;
    @Autowired
    private MessageSendLogService messageSendLogService;
    @Autowired
    private ConsumeLogService consumeLogService;

    /**
     * 平安 查询用户余额
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/userAcctQuery")
    @ResponseBody
    public UserAcctQueryResponse userAcctQuery(UserAcctQueryRequest userAcctQueryRequest) {
        UserAcctQueryResponse userAcctQueryResponse = new UserAcctQueryResponse();
        try {
            log.info("平安查询用户余额 接收参数：" + ToStringBuilder.reflectionToString(userAcctQueryRequest));
            String checkValidate = PAUserAcctQueryCommonProxy.checkUserAcctQuery(userAcctQueryRequest);
            if (StringUtils.isBlank(checkValidate)) {
                log.info("平安查询用户余额参数检查完毕");
                // 商户号由于是密文传输 此处解密
                String merchantId = userAcctQueryRequest.getUserId();
                BaseAcctInfoModel temp = baseAcctInfoService
                        .getModelByKey(merchantId);
                if (null != temp) {
                    userAcctQueryResponse.setReturnCode("0000");
                    userAcctQueryResponse.setReturnMsg("余额查询成功");
                    userAcctQueryResponse.setAvlBal(temp.getAvlBal().toString());
                } else {
                    userAcctQueryResponse.setReturnCode("0004");
                    userAcctQueryResponse.setReturnMsg("用户不存在");
                }
                log.info("平安查询用户余额处理成功");
            } else {
                log.info("输入参数不合法");
                userAcctQueryResponse.setReturnCode("0001");
                userAcctQueryResponse.setReturnMsg(checkValidate);
            }
        } catch (Exception e) {
            log.error("系统内部异常", e);
            userAcctQueryResponse.setReturnCode("9999");
            userAcctQueryResponse.setReturnMsg("系统内部异常");
        }
        return userAcctQueryResponse;
    }

    /**
     * 平安 短信发送接口
     * @return
     */
    @RequestMapping("/sendVerifyCode")
    @ResponseBody
    public SendVerifyCodeResponse sendVerifyCode(SendVerifyCodeRequest sendVerifyCodeRequest) {
        SendVerifyCodeResponse sendVerifyCodeResponse = new SendVerifyCodeResponse();
        try {
            log.info("平安 短信发送接口 接收参数："
                    + ToStringBuilder.reflectionToString(sendVerifyCodeRequest));
            String checkValidate = PASendVerifyCodeCommonProxy
                    .checkSendVerify(sendVerifyCodeRequest);
            if (StringUtils.isBlank(checkValidate)) {
                log.info("平安 短信发送接口参数检查完毕");
                String merchantId = sendVerifyCodeRequest.getUserId();
                MerchantInfoModel temp = merchantInfoService
                        .getModelByKey(merchantId);
                if (null != temp) {
                    // 传递手机号
                    MessageResponse response = MessageSendProxy.getMessageResponse(
                                    MobileCommonHelper.MESSAGETYPE_GAME, temp.getSysAccount());
                    sendVerifyCodeResponse.setReturnCode(response.getReturnCode());
                    sendVerifyCodeResponse.setReturnMsg(response.getErrMsg());
                } else {
                    sendVerifyCodeResponse.setReturnCode("0004");
                    sendVerifyCodeResponse.setReturnMsg("用户不存在");
                }
                log.info("平安短信发送接口处理成功");
            } else {
                log.info("输入参数不合法");
                sendVerifyCodeResponse.setReturnCode("0001");
                sendVerifyCodeResponse.setReturnMsg(checkValidate);
            }
        } catch (Exception e) {
            log.error("短信发送接口系统内部异常", e);
            sendVerifyCodeResponse.setReturnCode("9999");
            sendVerifyCodeResponse.setReturnMsg("系统内部异常");
        }
        return sendVerifyCodeResponse;
    }

    /**
     * 平安 充值接口
     * @return
     */
    @RequestMapping("/userAcctCharge")
    @ResponseBody
    public UserAcctChargeRepsonse userAcctCharge(UserAcctChargeRequest userAcctChargeRequest) {
        UserAcctChargeRepsonse userAcctChargeRepsonse = new UserAcctChargeRepsonse();
        try {
            log.info("平安充值接口接收参数："
                    + ToStringBuilder.reflectionToString(userAcctChargeRequest));
            String checkValidate = PAUserAcctChargeCommonProxy.checkUserAcctCharge(userAcctChargeRequest);
            if (StringUtils.isBlank(checkValidate)) {
                log.info("平安充值接口参数检查完毕");
                if (!messageSendLogService.checkVerifyCode(
                        userAcctChargeRequest.getTelephone(),
                        userAcctChargeRequest.getVerifyCode(),
                        MobileCommonHelper.MESSAGETYPE_GAME)) {
                    log.info("验证码错误");
                    userAcctChargeRepsonse.setReturnCode("0003");
                    userAcctChargeRepsonse.setReturnMsg("验证码错误");
                } else {
                    try {
                        String merchantId = userAcctChargeRequest.getUserId();
                        ConsumeLogModel parms = PAUserAcctChargeCommonProxy.transformUserAcctCharge(userAcctChargeRequest, merchantId);
                        consumeLogService.dealConsumeLog(parms, merchantId);
                        userAcctChargeRepsonse.setReturnCode("0000");
                        userAcctChargeRepsonse.setReturnMsg("充值处理成功");
                        userAcctChargeRepsonse.setOrderId(userAcctChargeRequest.getOrderId());
                        userAcctChargeRepsonse.setRechargeOrdId(parms.getConsumeId());
                        userAcctChargeRepsonse.setTradeAmt(parms.getAccessTradeAmt().toString());
                        log.info("平安充值接口接口处理成功");
                    } catch (GlobalServiceException e) {
                        log.info("平安充值异常", e);
                        userAcctChargeRepsonse.setReturnCode(e.getErrorCode());
                        userAcctChargeRepsonse.setReturnMsg(e.getErrorMsg());
                    }
                }
            } else {
                log.info("输入参数不合法");
                userAcctChargeRepsonse.setReturnCode("0001");
                userAcctChargeRepsonse.setReturnMsg(checkValidate);
            }
        } catch (Exception e) {
            log.error("充值接口系统内部异常", e);
            userAcctChargeRepsonse.setReturnCode("9999");
            userAcctChargeRepsonse.setReturnMsg("系统内部异常");
        }
        return userAcctChargeRepsonse;
    }

}
